package com.taotao.tools.codec.crc;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.codec.digest.PureJavaCrc32;


/**
 * <p>
 *
 * </p>
 *
 * @author youbeiwuhuan
 * @version 1.0.0
 * @date 2023/8/1 15:12
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class CrcUtils {

    public static int crc16(byte[] array) {
        if (array != null) {
            return CRC16.crc16(array);
        }

        return 0;
    }

    public static int crc32(byte[] array) {
        if (array != null) {
            return crc32(array, 0, array.length);
        }

        return 0;
    }

    public static int crc32(byte[] array, int offset, int length) {
        PureJavaCrc32 crc32 = new PureJavaCrc32();
        crc32.update(array, offset, length);
        return (int) (crc32.getValue() & 0x7FFFFFFF);
    }

}
